package utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import cassandra.RingStatistics;
import exception.RuleBasedMonitoringException;

public class ObjectPersistentUtil {
	final String filename = "RingStatictics.bytes";
	public void persistRingStatistics (RingStatistics rSta) throws RuleBasedMonitoringException {
		FileOutputStream fos = null;
		ObjectOutputStream out = null;
		try {
			String deploymentDir = ConfigProperties.getInstance().getDeploymentDir();
			fos = new FileOutputStream(deploymentDir + filename);
			out = new ObjectOutputStream(fos);
			out.writeObject(rSta);
			out.close();
		} catch (Exception e) {
			throw (new RuleBasedMonitoringException(e));
		} 

	}

	public RingStatistics recoverRingStatistics () throws RuleBasedMonitoringException {
		RingStatistics ring;
		FileInputStream fis = null;
		ObjectInputStream in = null;
		try {
			fis = new FileInputStream(filename);
			in = new ObjectInputStream(fis);
			ring = (RingStatistics) in.readObject();
			return ring;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw (new RuleBasedMonitoringException("Failed to recover ring statistics object from file", e));
		} 

	}


}
